import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-04-13
 * Time: 20:26
 */
public class MyQueue {
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    private int usedSize;
    public void offer(int val) {
        ListNode node = new ListNode(val);
        if(head == null) {
            head = node;
            last = node;
        } else {
            last.next = node;
            last = last.next;
        }
        usedSize++;
    }

    public int poll() {
        if(head == null) {
            return -1;
        }
        int val = 0;
        val = head.val;
        if(head.next == null) {
            head = null;
            last = null;
            return val;
        }
        head = head.next;
        usedSize--;
        return val;
    }
    public int peek() {
        if(head == null) {
            return -1;
        }
        return head.val;
    }
    public int size() {
        return usedSize;
    }
}
class MyCircularQueue {
    private int[] elem;
    private int front;
    private int rear;
    public MyCircularQueue(int k) {
        this.elem = new int[k + 1];
    }
    //入队
    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }

    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        int index = (rear == 0) ? elem.length - 1 : rear - 1;
        return elem[index];
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public boolean isFull() {
        return (rear + 1) % elem.length == front;
    }

    public static void main(String[] args) {
        MyCircularQueue myCircularQueue = new MyCircularQueue(3);
        myCircularQueue.enQueue(1);
        myCircularQueue.enQueue(2);
        myCircularQueue.enQueue(3);
        myCircularQueue.enQueue(4);
        System.out.println(myCircularQueue.Rear());
        System.out.println(myCircularQueue.isFull());
        System.out.println(myCircularQueue.deQueue());
        System.out.println(myCircularQueue.enQueue(4));
        System.out.println(myCircularQueue.Rear());
    }

}
class MyStack {
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    public MyStack() {
        this.queue1 = new LinkedList<>();
        this.queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if(!queue1.isEmpty()) {
            queue1.offer(x);
        } else if (!queue2.isEmpty()) {
            queue2.offer(x);
        } else {
            queue1.offer(x);
        }
    }

    public int pop() {
        if(empty()) {
            return -1;
        }else if (!queue1.isEmpty()) {
            int curSize = queue1.size();
            for (int i = 0; i < curSize - 1; i++) {
                int x = queue1.poll();
                queue2.offer(x);
            }
            return queue1.poll();
        }else {
            int curSize = queue2.size();
            for (int i = 0; i < curSize - 1; i++) {
                int x = queue2.poll();
                queue1.offer(x);
            }
            return queue2.poll();
        }
    }

    public int top() {
        if(empty()) {
            return -1;
        }else if (!queue1.isEmpty()) {
            int x = -1;
            int curSize = queue1.size();
            for (int i = 0; i < curSize; i++) {
                x = queue1.poll();
                queue2.offer(x);
            }
            return x;
        }else {
            int curSize = queue2.size();
            int x = -1;
            for (int i = 0; i < curSize; i++) {
                x = queue2.poll();
                queue1.offer(x);
            }
            return x;
        }
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }

    public static void main(String[] args) {
        MyStack myQueue = new MyStack();
        myQueue.push(1);
        myQueue.push(2);
        System.out.println(myQueue.pop());
        System.out.println(myQueue.top());

//        System.out.println(myQueue.empty());
    }
}
class MyQueue1 {
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;
    public MyQueue1() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        if(stack2.empty()) {
            while(!stack1.empty()) {
                int val = stack1.pop();
                stack2.push(val);
            }
        }
        return stack2.pop();
    }

    public int peek() {
        if(stack2.empty()) {
            while(!stack1.empty()) {
                int val = stack1.pop();
                stack2.push(val);
            }
        }
        return stack2.peek();
    }

    public boolean empty() {
        return stack1.empty() && stack2.empty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
